<template>


  <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" autocomplete="on" label-position="left">
    <div class="title-container">
      <h3 class="title">数字孪生PAAS平台后台管理</h3>
      <span class="title-small">Digital twin PAAS platform</span>
    </div>

    <el-card class="box-card" style="width:23.125rem;margin-left: 2.5rem;">
      <div slot="header" class="clearfix" >
        <span style="color: rgb(51, 51, 51);
            font-weight: bold;
            margin-bottom: 10px;
            display: block;
        ">用户登录</span>
        <div style="width: 100%;height: 1px;background: #d7d7d7;"></div>
      </div>
      <div style="margin-top: 0px;">
        <el-form-item prop="username">
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-input
            ref="username"
            v-model="loginForm.username"
            placeholder="请输入登录账号"
            name="username"
            type="text"
            tabindex="1"
            autocomplete="off"
          />
        </el-form-item>

        <el-tooltip v-model="capsTooltip" content="大写输入已打开" placement="right" manual>
          <el-form-item prop="password">
            <span class="svg-container">
              <svg-icon icon-class="password" />
            </span>
            <el-input
              :key="passwordType"
              ref="password"
              v-model="loginForm.password"
              :type="passwordType"
              placeholder="请输入登录密码"
              name="password"
              tabindex="2"
              autocomplete="off"
              @keyup.native="checkCapslock"
              @blur="capsTooltip = false"
              @keyup.enter.native="handleLogin"
            />
            <span class="show-pwd" @click="showPwd">
              <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
            </span>
          </el-form-item>
        </el-tooltip>
      </div>
      <el-button :loading="loading" type="primary custom-button" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleLogin">登录</el-button>
      <el-row :gutter="20" style="margin-top: -16px;">
        <el-col :span="16">
          <div style="text-align: left;">
            <a class="el-link-me" @click="handleReg">注册用户</a>
          </div>
        </el-col>
        <el-col :span="8">
          <div style="text-align: right;">
            <a class="el-link-me"  @click="handleForgetPwd">忘记密码</a>
          </div>
        </el-col>
      </el-row>
    </el-card>
  </el-form>



</template>

<script>
import { isBlank } from '@/utils/validate'


import { JSEncrypt } from 'jsencrypt'

export default {
  name: 'LoginForm',
  components: {  },
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!isBlank(value)) {
        callback(new Error('请填写正确的用户名！'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码至少6位！'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        // username: 'admin',
        // password: 'Xxc@admin2022',
        username: '',
        password: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      passwordType: 'password',
      capsTooltip: false,
      loading: false,
      showDialog: false,
      redirect: undefined,
      publicKey:'MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKyiOmdLfcrpepp8sbbFFDJ44NMEJUIAG9vTcD9S9YY9+H8INT804Y4W/egfdBdpm2AMbCs+rhWkETLjNqKJYdcCAwEAAQ==',
      otherQuery: {},
      regDialog: {
        visible: false,
        data: {}
      }
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        const query = route.query
        if (query) {
          this.redirect = query.redirect
          this.otherQuery = this.getOtherQuery(query)
        }
      },
      immediate: true
    }
  },
  created() {
  },
  mounted() {
    if (this.loginForm.username === '') {
      this.$refs.username.focus()
    } else if (this.loginForm.password === '') {
      this.$refs.password.focus()
    }
  },
  destroyed() {
  },
  methods: {
    checkCapslock(e) {
      const { key } = e
      this.capsTooltip = key && key.length === 1 && (key >= 'A' && key <= 'Z')
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/login',{
            username:this.encryptedData(this.loginForm.username),
            password:this.encryptedData(this.loginForm.password)
          })
            .then(() => {
              this.$router.push({ path: this.redirect || '/', query: this.otherQuery })
              this.loading = false
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== 'redirect') {
          acc[cur] = query[cur]
        }
        return acc
      }, {})
    },
    handleReg(){
        this.regDialog.visible = true
    },
    closeRegDialog: function(e) {
      this.regDialog.visible = false
    },
    //  加密
    encryptedData(data) {
      // 新建JSEncrypt对象
      let encryptor = new JSEncrypt();
      // 设置公钥
      encryptor.setPublicKey(this.publicKey);
      // 加密数据
      return encryptor.encrypt(data);
    },
    handleForgetPwd(){
      this.$emit('change', 'ForgetPwd')
    },
    handleReg(){
      this.$emit('change', 'Reg')
    }
  }
}
</script>


<style lang="scss" >
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#61a4aa;
$light_gray:#7f7f7f;
$cursor: #7f7f7f;

  @supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
    .login-container .el-input input {
      color: $cursor;
    }
  }


  .el-card__header {
    border: none;
  }

  .el-card__body {
      padding-top: 0px;
  }

  .el-link-me {
      color: #169bd5;
      text-decoration:none;
      font-size: 14px;
  }

  .el-link-me:hover {
      color: #4b4b4b;
      text-decoration:none;
  }

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 35px;
    width: 85%;

    input {
     /* background: transparent; */
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 30px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color:  $light_gray;
    background-color: white;
    border: 1px solid $light_gray;
  }
}
</style>

<style lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#7f7f7f;
$light_gray:#eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: #aaaaaa;
  overflow: hidden;


  .login-card {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }


  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 30px;
      color:#333333;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }

    .title-small{
      margin-top: -30px;
      display: block;
      margin-bottom: 35px;
      text-align: center;
      font-size: 25px;
      font-weight: bold;
      color: #333333;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  .thirdparty-button {
    position: absolute;
    right: 0;
    bottom: 6px;
  }

  @media only screen and (max-width: 470px) {
    .thirdparty-button {
      display: none;
    }
  }
}
</style>
